package com.yl.applicationsecurity.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yl.applicationsecurity.mapper.InspectionConfigurationMapper;
import com.yl.applicationsecurity.mapper.InspectionMapper;
import com.yl.applicationsecurity.pojo.Inspection;
import com.yl.applicationsecurity.pojo.InspectionConfiguration;
import com.yl.applicationsecurity.service.InspectionConfigurationService;
import com.yl.applicationsecurity.service.InspectionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author 鹤仙问鹿仙
 * @program: Application-security
 * @description:
 * @date 2025-01-08 14:21:19
 */
@Service
public class InspectionConfigurationServiceImpl extends ServiceImpl<InspectionConfigurationMapper, InspectionConfiguration> implements InspectionConfigurationService {


    @Override
    public PageInfo<InspectionConfiguration> findListByInspectionConfiguration(String[] nationSelect, InspectionConfiguration inspectionConfiguration, Date startCreateDate, Date endCreateDate, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize );//每页显示8条

        LambdaQueryWrapper<InspectionConfiguration> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        if ( StringUtils.isNotEmpty(inspectionConfiguration.getUrl())){
            lambdaQueryWrapper.like(InspectionConfiguration::getUrl,inspectionConfiguration.getUrl());
        }
        if ( StringUtils.isNotEmpty(inspectionConfiguration.getName())){
            lambdaQueryWrapper.like(InspectionConfiguration::getName,inspectionConfiguration.getName());
        }
        if ( StringUtils.isNotEmpty(inspectionConfiguration.getSystem())){
            lambdaQueryWrapper.like(InspectionConfiguration::getSystem,inspectionConfiguration.getSystem());
        }
        if ( StringUtils.isNotEmpty(inspectionConfiguration.getNation())){
            lambdaQueryWrapper.eq(InspectionConfiguration::getNation,inspectionConfiguration.getNation());
        }
        if (null!=inspectionConfiguration.getStatus()){
            lambdaQueryWrapper.eq(InspectionConfiguration::getStatus,inspectionConfiguration.getStatus());
        }

        if (null!=nationSelect&&nationSelect.length!=0){
            lambdaQueryWrapper.in(InspectionConfiguration::getNation,nationSelect);
        }

        if(startCreateDate!=null){
            lambdaQueryWrapper.ge(InspectionConfiguration::getCreateDate, startCreateDate);
        }


        if(endCreateDate!=null){
            lambdaQueryWrapper.le(InspectionConfiguration::getCreateDate, endCreateDate);
        }
        lambdaQueryWrapper.orderByDesc(InspectionConfiguration::getCreateDate);//根据首次发现时间排序

        List<InspectionConfiguration> list = baseMapper.selectList(lambdaQueryWrapper);


        PageInfo<InspectionConfiguration> pageInfo = new PageInfo<InspectionConfiguration>(list);


        if (pageInfo.getPages() == 0) {
            pageInfo.setPages(1);
        }

        return pageInfo;
    }

    @Override
    public int updateInspectionConfiguration(InspectionConfiguration inspectionConfiguration) {
        LambdaUpdateWrapper<InspectionConfiguration> updateWrapper = new LambdaUpdateWrapper<InspectionConfiguration>();
        updateWrapper.eq(InspectionConfiguration::getId, inspectionConfiguration.getId());
        updateWrapper.set(InspectionConfiguration::getUrl, inspectionConfiguration.getUrl());
        updateWrapper.set(InspectionConfiguration::getName, inspectionConfiguration.getName());
        updateWrapper.set(InspectionConfiguration::getStatus, inspectionConfiguration.getStatus());
        updateWrapper.set(InspectionConfiguration::getNation, inspectionConfiguration.getNation());
        updateWrapper.set(InspectionConfiguration::getReponeseFalseText, inspectionConfiguration.getReponeseFalseText());
        updateWrapper.set(InspectionConfiguration::getReponeseTrueText, inspectionConfiguration.getReponeseTrueText());
        updateWrapper.set(InspectionConfiguration::getRequtestText, inspectionConfiguration.getRequtestText());
        updateWrapper.set(InspectionConfiguration::getSystem, inspectionConfiguration.getSystem());
        updateWrapper.set(InspectionConfiguration::getEnv, inspectionConfiguration.getEnv());
        updateWrapper.set(InspectionConfiguration::getLastUpdateDate, new Date());

        return baseMapper.update(null,updateWrapper);
    }

    @Override
    public int updateStatus(String[] ids, Integer updateStatus) {
        LambdaUpdateWrapper<InspectionConfiguration> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.set(InspectionConfiguration::getStatus,updateStatus).in(InspectionConfiguration::getId , ids);
        if(ids==null||ids.length==0||updateStatus==null){
            return 1;
        }
        return baseMapper.update(null,lambdaUpdateWrapper);
    }

    @Override
    public int delete(String[] ids) {
        LambdaQueryWrapper<InspectionConfiguration> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(InspectionConfiguration::getId, ids);
        if(ids==null||ids.length==0){
            return 1;
        }
        return baseMapper.delete(lambdaQueryWrapper);
    }

    @Override
    public int insert(InspectionConfiguration inspectionConfiguration) {
        inspectionConfiguration.setId(UUID.randomUUID().toString());
        inspectionConfiguration.setCreateDate(new Date());
        return baseMapper.insert(inspectionConfiguration);
    }

    @Override
    public List<InspectionConfiguration> getList() {
        LambdaQueryWrapper<InspectionConfiguration> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(InspectionConfiguration::getStatus,1);

        lambdaQueryWrapper.lt(InspectionConfiguration::getEndSilenceTime,new Date()).or()
                .isNull(InspectionConfiguration::getEndSilenceTime); ;
        return baseMapper.selectList(lambdaQueryWrapper);
    }


}

